// pages/Settlement/OpenCard/OpenCard.js
const controllers = require('../../controllers/controllerIndex.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    click:true,
    selectItem:{},
    list:[
      {
        nick:"暂不选",
        id:""
      }
    ],
    index:"",
    staffId:"",
    buttons: [{text: '取消'}, {text: '确定'}],
    dialogShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.accountId){
      this.setData({
        'selectItem.nick':options.accountNick,
        'selectItem.mobile':options.accountMobile,
        'selectItem.accountId':options.accountId
      })
    }

    let that =this
    let vipList=[]

    


    controllers.vipShopList({
      type: 1,//0默认1商城页面分类列表2店铺入驻分类列表3系统新增商城分类
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('token'),
      state:1
    }).then(res =>{

      res.data.forEach(item =>{
        item.useTime=that.getDate(item.endTime)
        vipList.push(item);
        
      })
      this.setData({
        vipList: res.data,
        time:vipList,
      })
     
    })
  },
  bindKeyInput(res) {
    console.log(res)
    this.setData({
      input: res.detail.value
    })


  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false,
      click:true
    });
  },


  inputPhone(res){
    this.setData({

        phone:  res.detail.dataset
    })
  
 
  },
  inputName(res){
    this.setData({
      name:  res.detail.dataset
  })
  },
  optionTap(res) {
    var that = this;
    controllers.selectByMobile({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      mobile: this.data.input
    }).then(res => {
      that.setData({
        userList: res.data,
        click:false
      })

    })
  },
  selectUser(res){
  
  
    this.setData({
     
      selectItem: res.currentTarget.dataset.item,
      show:false
    })
   },


   
  onAddAccount() {
    let mobile = this.data.input||"";
    let url = '/pages/userStatistics/UserEdit/UserEdit?callback=onAccountResult&mobile='+mobile;
    wx.navigateTo({url:url})
  },
  onAccountResult(res) {
    let that = this;
    let params={
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      accountRelationId:res.id
    }
    controllers.accountDetail(params).then((res)=>{//转换对象
      let data = res.data;
      let item = {
        id:data.id,
        accountId:data.accountId,
        mobile:data.accountMobile,
        nick:data.accountNick
      };
      that.setData({
        selectId: item.id,
        selectItem: item,
        show:false
      });
    });

  },



  selectVip(res){
    console.log(res)
    this.setData({
     selectId:res.currentTarget.dataset.id
    })
  },
  selectType(res){
    console.log(res)
    this.setData({
      payType: res.currentTarget.dataset.type
    })
  },
  getDate(time){
   
    let newTime=new Date().getTime();
    let  lastDay =0
    if( time){
     
       let endTime=new Date(time.replace(/-/g,"/")).getTime();
      lastDay=Math.ceil((endTime-newTime)/1000/24/3600);
      
     
      return lastDay
      }else
      return lastDay
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  cancel() {
    wx.navigateBack({})
  },
  openCard() {
    let that =this
    if(that.data.selectItem.accountId==null){
      wx.showToast({
        icon:"none",
        title: '请选择用户',
        type: 'none'
      })
      return
    }
     
    if(that.data.selectId==null){
      wx.showToast({
        icon:"none",
        title: '请选择会员卡类型',
        type: 'none'
      })
      return
    }
    this.setData({
      dialogShow:true
    })
    // let json ={
    //   vipId:that.data.selectId,
    //   shopAccountId:that.data.staffId,
    //   shopId:wx.getStorageSync('shopId'),
    //   sessionId:wx.getStorageSync('token'),
    //   accountId:that.data.selectItem.accountId,
    // }
    // if( that.data.payType){
    //   json.payType=that.data.payType
    // }
    // controllers.openVip(json).then(res =>{
  
    //   wx.navigateBack({
    
    //     success: ()=>{
    //       wx.showToast({
    //         title: '开卡成功'
    //       });
    //     }
    //   })
    // }
    // )

   
    // wx.navigateTo({
    //   url: '../vipOpenCard/VipOpenCard',
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStaffList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }

  //获取员工列表
  getStaffList(){
    let shopId=wx.getStorageSync('shopId')
    let list=this.data.list
    controllers.getStaffList({shopId:shopId}).then(res=>{
      list=list.concat(res.data)
      this.setData({
        list:list
      })
    })
  },

  //绑定员工
  bindStaff(e){
    let index=e.detail.value
    this.setData({
      index:index,
      staffId:this.data.list[index].id,
      staffName:this.data.list[index].nick
    })
  },

  tapDialogButton(e){
    let index=e.detail.index;
    let that=this
    if(index==1){
      let json ={
        vipId:that.data.selectId,
        shopAccountId:that.data.staffId,
        shopId:wx.getStorageSync('shopId'),
        sessionId:wx.getStorageSync('token'),
        accountId:that.data.selectItem.accountId,
      }
      if( that.data.payType){
        json.payType=that.data.payType
      }
      controllers.openVip(json).then(res =>{
            wx.showToast({
              title: '开卡成功'
            });
        }
      )
    }else{
      
    }
    this.setData({
      dialogShow:false
    })
  }
})